﻿import re

import logging
from datetime import datetime

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.api import mail


from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write(self.reqContent(self.request, urlfetch.GET))
        
    def post(self):
        logging.info("req_full_path 1:"+self.request.url)
        self.response.out.write(self.reqContent(self.request, urlfetch.POST))
        
    def reqContent(self, request, method=urlfetch.GET):
        logging.info('*'*100)
        data = None
        if method == urlfetch.POST:
            data = request.body

        host = "http://www.newsmth.net"
        url = ""
        req_full_path = request.url
        logging.info("req_full_path:"+req_full_path)
        req_full_path = req_full_path[req_full_path.find('/a/'):]
        logging.info("req_full_path:"+req_full_path)
        if req_full_path=='/a/' or req_full_path.startswith('/a/?'):
            req_full_path = req_full_path[req_full_path.find('?'):]
            url = host + "/atomic2.php" + req_full_path
        else:
            req_full_path = req_full_path[req_full_path.find('/', 1):]
            url = host + req_full_path
        
        htmlcontent = ""
        htmlcontent = self.httpRequest(url, method, data)
        #logging.info(htmlcontent)
        
        return htmlcontent
        
    def httpRequest(self, url, method=urlfetch.GET, data=None):
        count = 1;
        while(count < 3):
            response = None
            try:
                #urlfetch.REDIRECT_STATUSES = frozenset([httplib.SEE_OTHER])
                response = urlfetch.fetch(url=url, follow_redirects=False, method=method, payload=data)
                
                redirect_counts = 0
                while redirect_counts < 5 and response and response.status_code in [301,302]:
                    logging.info(str(count)+"--"+url+", status_code:"+str(response.status_code))
                    if response.headers.has_key('location'):
                        url = response.headers['location']
                    elif response.headers.has_key('Location'):
                        url = response.headers['Location']
                    else:
                        raise Exception('Could not find location in headers: %r' % (response.headers,))
                    global smthhost
                    url = smthhost + '/' +url
                    #logging.info("new url:"+url)
                    response = urlfetch.fetch(url=url, follow_redirects=False)
                    redirect_counts += 1

            except:
                logging.info(traceback.format_exc())
                count = count + 1
                continue
            logging.info(str(count)+"--"+url+", status_code:"+str(response.status_code))
            if response and response.status_code==200:
                return converFromGB2312ToUTF8(response.content)
            count = count + 1

def main():
    application = webapp.WSGIApplication([('/a/.*', MainPage)], debug=True)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

    
    
# ##########################################################################################################################
# /


